import java.util.Arrays;

public class Shuzu {
    public static int binarySearch(int[] array,int key) {
        int left = 0;
        int right = array.length-1;
        while(left <= right) {
            int mid = (left + right) / 2;
            if(array[mid] < key) {
                left = mid + 1;
            }else if(array[mid] > key) {
                right = mid - 1;
            }else {
                return mid;
            }
        }
        return -1;
    }
    public static int findVal(int[] array,int x) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] == x){
                return i;
            }
        }
        return -1;
    }
    public static void bubbleSort(int[] array){
                                   //传哈希值
        //i表示的是趟数
        for (int i = 0; i < array.length - 1; i++) {
            //j表示的是每一趟比较的次数
            boolean flg = false;//用旗来判断有没有发生
            for (int j = 0; j < array.length - 1 -i; j++) {
                if(array[j] > array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                    flg = true;
                }
            }
            if(flg == false){
                break;//说明上一趟时就已经有序了，不要再进行了
            }
        }
    }
    public static void main(String[] args) {
        int[] array = {1,2,3,13,4,15};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));

        Arrays.sort(array);//排序数组
        System.out.println(Arrays.toString(array));//数组哈希值的统一用将数组内容化成字符串的方法打印字符串的方法打印显示里面的内容
        System.out.println(Arrays.binarySearch(array, 15));//查找数据返回查找数据的下标

        System.out.println("========================");

        int[] array2 = {1,2,130,4,15};
        int[] array3 = {1,2,13,4,15};
        boolean flg = Arrays.equals(array2,array3);//比较两数组是否相等
        System.out.println(flg);

        System.out.println("======================");

        int[] array4 = new int[10];
        Arrays.fill(array4,1);//数组填充数据
        System.out.println(Arrays.toString(array4));
        Arrays.fill(array4,1,4,-1);
        System.out.println(Arrays.toString(array4));

        System.out.println("======================");

        System.out.println(binarySearch(array3, 5));
        System.out.println(Arrays.binarySearch(array3, 5));//数组的二分查找方法,返回一个负数
        System.out.println(findVal(array3, 5));//返回-1

        System.out.println("======================");

        int[] copy = new int[array.length*2];
        System.arraycopy(array,0,copy,0,array.length);//用系统的方法拷贝数组内容
        System.out.println(Arrays.toString(copy));

        //此时就相当于新数组扩容，能拷贝多少拷贝多少
        int[] copy2 = Arrays.copyOf(array,array.length*2);//用数组工具的方法拷贝，创建的是新数组
        System.out.println(Arrays.toString(copy2));
        int[] copy3 = Arrays.copyOfRange(array,1,13);//[1,13)
        System.out.println(Arrays.toString(copy3));
    }
    public static void main3(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] copy = new int[array.length];//现在全是0
        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        System.out.println(copy);//copy数组变量里存的是哈希值，打印是打印变量里存的内容
        System.out.println(Arrays.toString(copy));
        int a = 10;
        System.out.println(a);//变量.sout 打印变量里的值快捷键

        int[] array2 = {1,2,3,4,5};
        int[] array3 = array2;//不是拷贝
        System.out.println(Arrays.toString(array3));
    }
    public static int[] func(){
        return new int[]{1,2,3,4,5};
    }     //这个整体就是个方法，方法返回值哈希值
    public static String myToString(int[] array) {
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
            if(i != array.length-1){
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }
    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5};
        String ret = myToString(array);
        System.out.println(ret);
        System.out.println(myToString(array));
        int[] ret2 = func();
        System.out.println(ret2);//println返回值void
        System.out.println(Arrays.toString(ret2));
        int[] array3 = null;
        /*System.out.println(array3.length);*/ //err
    }
    public static int max(int a, int b){
        return a > b ? a : b;
    }
    public static int max(int a, int b, int c){//方法重载
        int max = max(a,b);
        return max(max,c);
    }
    public static void main1(String[] args) {
        int[] array = {1,2,3,4};
        for (int i = 0; i <array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
        for (int x:array
             ) {
            System.out.print(x+" ");
        }
        System.out.println();
    }
}
